﻿using LitJson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class ShipEvent
{
    public KeyValuePair<List<List<ShipEventTrigger>>, List<ShipEventBehaviours>> m_Events = new KeyValuePair<List<List<ShipEventTrigger>>, List<ShipEventBehaviours>>(new List<List<ShipEventTrigger>>(), new List<ShipEventBehaviours>());
    public string m_Name = "NONE";
    public ShipEvent()
    {
    }
    
    public void BindShip(Ship ship)
    {
        var triggersOr = m_Events.Key;
        foreach (var triggersAnd in triggersOr)
        {
            foreach (var trigger in triggersAnd)
            {
                trigger.BindShip(ship);
            }
        }
        var behaviours = m_Events.Value;
        foreach (var i in behaviours)
        {
            i.BindShip(ship);
        }
    }

    //  当舰船被创建的时候调用
    public void Init()
    {
        var triggersOr = m_Events.Key;
        foreach (var triggersAnd in triggersOr)
        {
            foreach(var trigger in triggersAnd)
            {
                trigger.Init();
            }
        }

        var behaviours = m_Events.Value;
        foreach(var i in behaviours)
        {
            i.Init();
        }
    }
    public void Update()
    {
        bool isTure = false;
        foreach(var i in m_Events.Key)
        {
            foreach (var j in i)
            {
                isTure = true;
                if (j.CheckTrigger() == false)
                {
                    isTure = false;
                    break;
                }
            }
            if (isTure == true)
                break;
        }

        if (isTure == false)
            return;

        foreach(var i in m_Events.Value)
        {
            i.Execute();
        }
    }


    public JsonData Save()
    {
        JsonData root = new JsonData();

        root["Name"] = m_Name;

        root["Triggers"] = new JsonData();
        root["Triggers"].SetJsonType(JsonType.Array);
        foreach(var or in m_Events.Key)
        {
            var orJson = new JsonData();
            orJson.SetJsonType(JsonType.Object);

            var andJson = new JsonData();
            andJson.SetJsonType(JsonType.Array);

            foreach (var and in or)
            {
                andJson.Add(and.Save());
            }
            orJson["TriggerAnd"] = andJson;
            root["Triggers"].Add(orJson);
        }
        root["Behaviours"] = new JsonData();
        root["Behaviours"].SetJsonType(JsonType.Array);
        foreach(var behaviour in m_Events.Value)
        {
            root["Behaviours"].Add(behaviour.Save());
        }
        return root;
    }

    public bool Load(JsonData root)
    {
        IDictionary<string, JsonData> rootObj = root.ValueAsObject();

        if(rootObj.ContainsKey("Name"))
            m_Name = rootObj["Name"].ValueAsString();

        if (rootObj.ContainsKey("Triggers"))
        {
            var triggerOrJson = rootObj["Triggers"].ValueAsArray();
            for (int i = 0; i < triggerOrJson.Count; ++i)
            {
                IDictionary<string, JsonData> andTriggerObj = triggerOrJson[i].ValueAsObject();
                if (!andTriggerObj.ContainsKey("TriggerAnd"))
                    continue;
                List<ShipEventTrigger> andTriggers = new List<ShipEventTrigger>();
                var triggerAndJson = andTriggerObj["TriggerAnd"].ValueAsArray();
                for(int j=0; j< triggerAndJson.Count; ++j)
                {
                    var trigger = ShipEventTrigger.Create(triggerAndJson[j]);
                    if(trigger != null)
                        andTriggers.Add(trigger);
                }
                if(andTriggers.Count > 0)
                    m_Events.Key.Add(andTriggers);
            }
        }

        if (rootObj.ContainsKey("Behaviours"))
        {
            var behaviourJson = rootObj["Behaviours"].ValueAsArray();
            for (int i = 0; i < behaviourJson.Count; ++i)
            {
                var behaviour = ShipEventBehaviours.Create(behaviourJson[i]);
                if(behaviour != null)
                    m_Events.Value.Add(behaviour) ;
            }
        }
        return true;
    }
}